Skip to content

feat(mappers): langfuse provider changes to support newer version of langfuse#165

Open
poshinchen wants to merge 2 commits intostrands-agents:mainfrom
poshinchen:feat/mappers-langfuse-change
Open

feat(mappers): langfuse provider changes to support newer version of langfuse#165
poshinchen wants to merge 2 commits intostrands-agents:mainfrom
poshinchen:feat/mappers-langfuse-change

Conversation

@poshinchen
Copy link
Contributor

@poshinchen poshinchen commented Mar 16, 2026

Description

LangfuseProvider multi-framework support + Langfuse v4 compatibility

  • Updated LangfuseProvider to support newer version of langfuse SDK since the callback feature is added after v4
  • Makes LangfuseProvider work with traces from any framework (LangChain, Strands, LlamaIndex, CrewAI, etc.) — not just Strands — and adds support for the Langfuse v4 SDK.

Previously, LangfuseProvider only handled Strands-specific observation names ("execute_tool", "invoke_agent"). Traces from LangChain or other frameworks were silently skipped. This PR makes the provider framework-agnostic by routing on obs.type (universal across all frameworks in Langfuse) with a Strands-specific fallback for backward compatibility.

Changes

  • langfuse_provider.py — Multi-framework observation routing + Langfuse v4 support:
    • _convert_observation(): Route by universal obs.type (GENERATION → InferenceSpan, TOOL → ToolExecutionSpan, CHAIN/AGENT root → AgentInvocationSpan) with Strands SPAN name-based fallback preserved
    • _convert_tool_execution(): Handle both Strands format (obs.input.name) and LangChain/universal format (obs.name + obs.input as arguments)
    • _parse_tool_result(): Support LangChain ToolMessage format ({"content": "...", "type": "tool"})
    • _convert_message(): Parse LangChain tool_calls field on assistant messages
    • _extract_user_prompt(): Handle LangChain CHAIN formats ({"input": "..."}, {"messages": [...]}) and Strands message-list format via OTEL ([{"role": "user", "content": '[{"text": "..."}]'}])
    • _extract_agent_response(): Handle LangChain CHAIN output formats ({"output": "..."}, {"messages": [...]}) and Strands message-list output format
    • _extract_text_from_content(): New helper to unwrap Strands content (plain string, JSON-encoded [{"text": "..."}], or parsed list)
    • _fetch_observations(): Use legacy.observations_v1.get_many() on Langfuse v4+ (v4's new endpoint returns lightweight objects missing input/output/name)
    • _fetch_all_pages(): Auto-detect page-based (v2/v3: meta.total_pages) vs cursor-based (v4+: meta.cursor) pagination
  • pyproject.toml: Bump langfuse dependency from <3 to <5 to support v4 SDK

Backward compatibility

  • Strands traces: the SPAN + obs.name fallback is preserved — existing Strands→Langfuse flows work unchanged
  • Langfuse v2/v3 SDK: page-based pagination and observations.get_many() paths are preserved
  • All 27 pre-existing tests continue to pass

Related Issues

#91
#159

Documentation PR

N/A

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@poshinchen poshinchen force-pushed the feat/mappers-langfuse-change branch from 908985f to 51db383 Compare March 23, 2026 00:26
@poshinchen poshinchen changed the title Feat/mappers langfuse change feat(mappers) Feat/mappers langfuse change to support newer version of langfuse Mar 23, 2026
@poshinchen poshinchen changed the title feat(mappers) Feat/mappers langfuse change to support newer version of langfuse feat(mappers): Feat/mappers langfuse change to support newer version of langfuse Mar 23, 2026
@poshinchen poshinchen changed the title feat(mappers): Feat/mappers langfuse change to support newer version of langfuse feat(mappers): langfuse provider changes to support newer version of langfuse Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants